package org.spring.springboot.util;

import java.io.*;
import org.springframework.web.multipart.MultipartFile;

public class UploadImageUtil {

    /**
     * 图片上传
     *
     * @param file
     * @param uploadFileName
     * @param fpath
     * @return
     * @throws Exception
     */
    public void uploadImage(MultipartFile file, String uploadFileName, String fpath) throws Exception {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream((File) file);
            File f = new File(fpath);
            if (!f.exists()) f.mkdirs();
            os = new FileOutputStream(fpath + "/" + uploadFileName);
            byte buffer[] = new byte[1024];
            int count = 0;
            while ((count = is.read(buffer)) > 0) {
                os.write(buffer, 0, count);
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            File f = new File(fpath + "/" + uploadFileName);
            if (f.exists()) {
                f.delete();
            }
        } finally {
            try {
                os.close();
                is.close();
            } catch (IOException ioe) {
            }
        }
    }

}

